/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.command.executor;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SystemMessage;
public class L1Report implements L1CommandExecutor {
	private static Logger _log = Logger.getLogger(L1Report.class.getName());

	private L1Report() {}

	public static L1CommandExecutor getInstance() {
		return new L1Report();
	}

	@Override
	public void execute(L1PcInstance pc, String cmdName, String arg) {
		if (arg.isEmpty()) {
			pc.sendPackets(new S_SystemMessage("請輸入 : ."+ cmdName + " 問題內容。"));
			return;
		}
		
		String msg = null;
		Connection con = null;
		PreparedStatement pstm = null;
		try {
			con = DriverManager.getConnection( 
				      "jdbc:mysql://localhost/lib_db?useUnicode=true&characterEncoding=utf8", 
				      "root", "root");
			pstm = con.prepareStatement("INSERT report SET login=?,player=?,LocX=?,LocY=?,MapID=?,detail=?,date=?");
			
			pstm.setString(1, "");
			pstm.setString(2, pc.getName());
			pstm.setInt(3, pc.getLocation().getX());
			pstm.setInt(4, pc.getLocation().getY());
			pstm.setInt(5, pc.getLocation().getMapId());
			pstm.setString(6, arg);
			pstm.setTimestamp(7, new Timestamp(System.currentTimeMillis()));
			pstm.execute();
			
			pc.sendPackets(new S_SystemMessage("問題回報成功。"));
		} catch (Exception e) {
			_log.log(Level.SEVERE, "", e);
			msg = "請輸入 : ."+ cmdName + " 問題內容。";
		} finally {
			if (msg != null) {
				pc.sendPackets(new S_SystemMessage(msg));
			}
		}
	}
}
